<script lang="ts" setup name="Goods">
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { useRoute } from 'vue-router'
import { ref, watchEffect } from 'vue'
const { goods } = useStore()
const { goodsInfo } = storeToRefs(goods)
const route = useRoute()
watchEffect(() => {
  if (route.params.id && route.path === `/goods/${route.params.id}`) {
    // 清除之前的信息 => id发生改变时需要重置信息（因为id如果存在 组件重新加载时内部的方法是不会销毁的）
    goods.resetGoodsInfo()
    // 重新获取信息
    goods.getGoodsInfo(route.params.id as string)
  }
})

// 父传子将选择的sku进行获取和更新
const checkedGoods = (skuId: string) => {
  const sku = goodsInfo.value.skus.find((sku) => sku.id === skuId)
  if (sku) {
    goodsInfo.value.inventory = sku.inventory
    goodsInfo.value.oldPrice = sku.oldPrice
    goodsInfo.value.price = sku.price
  }
}

// 数字框
const num = ref(1)
</script>
<template>
  <div class="xtx-goods-page"  v-if="goodsInfo.id">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <template>
          <XtxBreadItem to="/">{{ goodsInfo.categories[1].name }}</XtxBreadItem>
          <XtxBreadItem to="/">{{ goodsInfo.categories[0].name }}</XtxBreadItem>
          <XtxBreadItem>{{ goodsInfo.name }}</XtxBreadItem>
        </template>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧 -->
        <div class="media">
          <!-- 放大镜 -->
          <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
          <!-- 销量/评价/收藏 -->
          <GoodsSales :goodsInfo="goodsInfo"></GoodsSales>
        </div>
        <!-- 右侧 -->
        <div class="spec">
          <!-- 商品介绍 + 城市选择 -->
          <GoodName :goodsInfo="goodsInfo"></GoodName>
          <!-- sku -->
          <GoodsSku
            :goodsInfo="goodsInfo"
            :skuId="goods.filterSkus[0].id"
            @checkedGoods="checkedGoods"
          ></GoodsSku>
          <!-- 计数器 -->
          <XtxNumbox
            v-model="num"
            :min="1"
            :max="goodsInfo.inventory"
            label="数量"
          ></XtxNumbox>
          <!-- 加入购物车 -->
          <XtxButton type="primary" size="middle" style="margin-top: 20px">
            加入购物车
          </XtxButton>
        </div>
      </div>

      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="goodsInfo"></GoodsDetail>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
